package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.sky.dto.*;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 订单搜素
     */
    @GetMapping("/conditionSearch")
    public Result<PageResult> selectOrder(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("前端要搜索的订单：{}", ordersPageQueryDTO);
        PageResult page = orderService.selectOrder(ordersPageQueryDTO);
        return Result.success(page);
    }

    /**
     * 查询订单详情
     */
    @GetMapping("/details/{id}")
    public Result<OrderVO> selectOrderDetails(@PathVariable Long id) {
        log.info("前端要查看的订单ID：{}", id);
        OrderVO orderVO = orderService.selectOrderDetails(id);
        return Result.success(orderVO);
    }

    /**
     * 接单
     */
    @PutMapping("/confirm")
    public Result acceptOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        log.info("接取的订单ID：{}", ordersConfirmDTO);
        orderService.acceptOrder(ordersConfirmDTO);
        return Result.success();
    }

    @PutMapping("rejection")
    public Result rejectOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        log.info("商家拒单的ID及原因：{}", ordersRejectionDTO);
        orderService.rejectOrder(ordersRejectionDTO);
        return Result.success();
    }

    /**
     * 各个状态的订单数量统计
     */
    @GetMapping("/statistics")
    public Result<OrderStatisticsVO> orderStatistics () {
        log.info("统计各个状态的订单数量");
        OrderStatisticsVO result = orderService.orderStatistics();
        return Result.success();
    }

        /*    业务规则：
    - 待支付和待接单状态下，用户可直接取消订单
    - 商家已接单状态下，用户取消订单需电话沟通商家
    - 派送中状态下，用户取消订单需电话沟通商家
    - 如果在待接单状态下取消订单，需要给用户退款
    - 取消订单后需要将订单状态修改为“已取消 */
    /**
     * 取消订单
     */
    @PutMapping("/cancel")
    public Result adminCancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        log.info("商家取消订单的ID及其原因：{}", ordersCancelDTO);
        orderService.adminCancelOrder(ordersCancelDTO);
        return Result.success();
    }

    /**
     * 派送订单
     */
    @PutMapping("/delivery/{id}")
    public Result deliveryOrders(@PathVariable Long id) {
        log.info("即将派送的订单ID：{}", id);
        orderService.deliveryOrders(id);
        return Result.success();
    }

    /**
     * 完成订单
     */
    @PutMapping("/complete/{id}")
    public Result completeOrders(@PathVariable Long id) {
        log.info("完成的订单ID：{}", id);
        orderService.completeOrders(id);
        return Result.success();
    }
}
